﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 21/12/2008
// Time: 14:16
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of State.
	/// </summary>
	public class State : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region private declarations 
			private String _country_id;
			private String _name;
			private String _sigla;
			private String _create_date;
			private String _enable;	
		#endregion
			
		public State()
		{
			gsql.table = "common.state";
			_sequence  = "common.state_state_id_seq";
		}
		
		#region properties
			public string countryId 
			{
				get { return _country_id; }
				set { _country_id = value; }
			}
			
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string sigla 
			{
				get { return _sigla; }
				set { _sigla = value; }
			}
		
			public string createDate 
			{
				get { return _create_date; }
				set { _create_date = value; }
			}
			
			public Boolean enable 
			{
				get { return Convert.ToBoolean(_enable); }
				set { _enable = Convert.ToString(value); }
			}		
		#endregion
		
		public void clear()
		{
			_id 			= String.Empty;
			_country_id = String.Empty;
			_name 		= String.Empty;
			_sigla 		= String.Empty;
			_enable		= Boolean.TrueString;
			_create_date= String.Empty;			
		}
		
		public override void get(DataRow dr)
		{
			_id 			= dr["state_id"].ToString();
			_country_id = dr["state_country_id"].ToString();
			_name 		= dr["state_name"].ToString();
			_sigla 		= dr["state_sigla"].ToString();
			_enable		= dr["state_enable"].ToString();
			_create_date= Convert.ToDateTime(dr["state_create_date"]).ToString(common.CONSTANTS.dateFormatBrazilian);
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id 		     ,"state_id");			  
			gsql.setValue(_country_id    ,"state_country_id");			  
			gsql.setValue(_name 		     ,"state_name");			  
			gsql.setValue(_sigla 	     ,"state_sigla");			  
			gsql.setValue(_enable		  ,"state_enable");			  
			//gsql.setValue(_create_date   ,"state_create_date");	
		}
	}
	
	public class States : DefaultList, IList
	{
		public States()
		{
			gsql.table = "common.state";
		}
			
		/*public List<State> items
		{
			get { return items; }
		}*/
				
		public State get(Int32 index)
		{
			return (State)items[index];
		}
		
		public State get()
		{
			return (State)items[index];
		}		
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY state_name";
			
			return mountList();
		}
		
		public bool fillList(String countryId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere= "state_country_id = "+countryId;
			gsql.additionalWhere = "ORDER BY state_name";
			
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam = gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				State s = new State();
				s.get(gsql.getRow(i));
				items.Add(s);
			}
			return true;
		}
	}
}
